package com.bdcom.autotest.ui.snp;

import java.awt.Color;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import javax.swing.JPanel;
import javax.swing.JTextField;

public abstract class AbsPanel extends JPanel implements ISNGetSet{
	
	public  LinkedHashMap<String , JTextField> numToSNField = new LinkedHashMap<String, JTextField>();
	public LinkedHashMap<String , JTextField> numToTField = new LinkedHashMap<String, JTextField>();
	public JTextField workNoField = new JTextField();
	@Override
	public String[] getSNs() {
		Collection<JTextField> values = numToSNField.values();
		String[] arrs = new String[ values.size()  ];
		int i=0;
		for(   JTextField  ff:  values) {
			arrs[i++] = ff.getText().trim();
		}
		return arrs;
	}


	@Override
	public void setSNs(String[] sns) {
		
		int cnt = 0;
		Set<Entry<String,JTextField>> entrySet = numToSNField.entrySet();
		for (  Entry<String,JTextField> en : entrySet  ) {
			JTextField value = en.getValue();
			String v = "";
			if(  sns!=null  ) {
				v = sns[cnt++];
			}
			value.setText(v);
		}
		
	}

	@Override
	public String[] getTypes() {
		
		Collection<JTextField> values = numToTField.values();
		String[] arrs = new String[ values.size()  ];
		int i=0;
		for(   JTextField  ff:  values) {
			arrs[i++] = ff.getText().trim();
		}
		return arrs;
		
	}


	@Override
	public void setTypes(String[] types) {
		int cnt = 0;
		Set<Entry<String,JTextField>> entrySet = numToTField.entrySet();
		for (  Entry<String,JTextField> en : entrySet  ) {
			JTextField value = en.getValue();
			String v = "";
			if(  types!=null  ) {
				v = types[cnt++];
			}
			value.setText(v);
		}
	}


	@Override
	public void lockField(boolean isEnable) {
		Collection<JTextField> values = numToSNField.values();
		for(  JTextField  v : values   ) {
			v.setEnabled(isEnable);
		}
		values = numToTField.values();
		for(  JTextField  v : values   ) {
			v.setEnabled(isEnable);
		}
	}
	
	@Override
	public void setFail(List<String> sns) {
		Collection<JTextField> values = numToSNField.values();
		for(JTextField value:values){
			String sn = value.getText().trim();
			if(!sns.contains(sn)&&!"".equals(sn)){
				value.setBackground(new Color(255, 0, 0));
			}else{
				value.setBackground(new Color(255, 255, 255));
			}
		}
	}

	@Override
	public void setResult(List<String> sns) {
		Collection<JTextField> values = numToSNField.values();
		for(JTextField value:values){
			String sn = value.getText().trim();
			if(!sns.contains(sn)&&!"".equals(sn)){
				value.setBackground(new Color(255, 0, 0));
			}else{
				value.setBackground(new Color(0, 255, 0));
			}
		}
	}

	@Override
	public void setWorkNo(String workNo) {
		workNoField.setText(workNo);
	}

	@Override
	public String getWorkNo() {
		return workNoField.getText();
	}
}
